package org.mountainpunch.ds.sparsematrix;

/**
 * A matrix data for the SparseMatrix which has float as its data.
 * @author siddharth
 */
public class FloatMatrixData implements MatrixDataOperable {
  private float _data ;

  /**
   * Constructs a MatrixData which contains float as its data.
   * @param data the float data.
   */
  public FloatMatrixData(float data) {
    _data = data;
  }

  /**
   * {@inheritDoc }
   */
  @Override
  public MatrixDataOperable multiply(MatrixDataOperable mdata) {
    if (mdata instanceof FloatMatrixData) {
      FloatMatrixData imd = (FloatMatrixData) mdata;
      return new FloatMatrixData(_data * imd.getData());
    }

    throw new RuntimeException("MatrixDataOperable not found");
  }

  /**
   * {@inheritDoc }
   */
  @Override
  public MatrixDataOperable add(MatrixDataOperable mdata) {
    if(mdata == null) {
      return new FloatMatrixData(_data) ;
    }

    if (mdata instanceof FloatMatrixData) {
      FloatMatrixData imd = (FloatMatrixData) mdata;
      return new FloatMatrixData(_data + imd.getData());
    }

    throw new RuntimeException("MatrixDataOperable not found");
  }

  /**
   * {@inheritDoc }
   */
  @Override
  public MatrixDataOperable subtract(MatrixDataOperable mdata) {

    if(mdata == null) {
      return new FloatMatrixData(_data) ;
    }
    
    if (mdata instanceof FloatMatrixData) {
      FloatMatrixData imd = (FloatMatrixData) mdata;
      return new FloatMatrixData(_data - imd.getData());
    }

    throw new RuntimeException("MatrixDataOperable not found");
  }

  /**
   * {@inheritDoc }
   */
  @Override
  public MatrixDataOperable interpolate(MatrixDataOperable mdata, float t) {
    if(mdata == null) {
      throw new NullPointerException("MatrixDataOperable operand is null");
    }

    if (mdata instanceof FloatMatrixData) {
      FloatMatrixData imd = (FloatMatrixData) mdata;
      return new FloatMatrixData(_data + t*( imd.getData() - _data));
    }

    throw new RuntimeException("MatrixDataOperable not found");
  }

  /**
   * Gets the data contained in this MatrixData.
   * @return the data.
   */
  public float getData() {
    return _data ;
  }

  /**
   * {@inheritDoc }
   */
  @Override
  public String toString() {
    return String.valueOf(_data);
  }

  /**
   * {@inheritDoc }
   */
  public MatrixDataOperable negate() {
    return new FloatMatrixData(-_data) ;
  }
}
